/**
 * Copyright (c) 2013-2022 Nikita Koksharov
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson.spring.starter;

import com.sunday.redisson.lock.DistributedLock;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisOperations;

/**
 * 适配 redis 的 lettuce jedis等驱动，优先采用这两个构建 redisTemplate/stringRedisTemplate
 * 同时适配 Spring Boot 2.7+ 的升级改为该类进行处理
 * <p>
 * Redisson 使用注意:
 * <p>
 * https://github.com/redisson/redisson/wiki/Table-of-Content
 * <p>
 * 1. eval指令存在威胁性，一般系统会将eval指令改写，防止被攻击时轻易操作
 * 如果要进行改写请 org.redisson.client.protocol.RedisCommands 进行
 * <p>
 * 2. redis驱动分为 jedis lettuce 和 redisson, 个人比较推荐 lettuce作为 redisTemplate/stringRedisTemplate 的驱动源，
 * 而redisson仅作为操作分布式锁的源，因此改写注释，将redisson的加载至于标准的 RedisAutoConfiguration 之后进行.
 * '@AutoConfiguration(before = RedisAutoConfiguration.class) -> @AutoConfiguration(after = RedisAutoConfiguration.class)
 * <p>
 * 3. 生成DistributedLock对象，进行分布式锁操作
 * '@Bean
 * public DistributedLock distributedLock(RedissonClient redissonClient) {
 * return new DistributedLock(redissonClient);
 * }
 * <p>
 * 4. yaml配置文件填写， com.sunday.common.redisson.codec.GsonCodec为 Gson 序列化编写
 * codec: !<com.sunday.common.redisson.codec.GsonCodec> {}
 * 其实与
 * <p>
 * 5. YAML文件的配置
 * Redisson配置可以以YAML格式存储。
 * 使用config.fromYAML方法读取以YAML格式存储的配置:
 * redis:
 * redisson:
 * file: 'redisson.yml'
 *
 * @author zsy
 * @since 2024/1/24
 */
@AutoConfiguration(after = RedisAutoConfiguration.class)
@ConditionalOnClass({Redisson.class, RedisOperations.class})
@EnableConfigurationProperties({RedissonProperties.class, RedisProperties.class})
public class RedissonAutoConfigurationV2 extends RedissonAutoConfiguration {

    @Bean
    public DistributedLock distributedLock(RedissonClient redissonClient) {
        return new DistributedLock(redissonClient);
    }

}
